<?php
/**
 *
 */
class video_propModel extends NewModel
{
    public function getNewestOne($field = '', $where = '', $order = 'id desc')
    {
        return $this->table('video_prop_' . date('Ym'))->field($field)->order($order)->selectOne($where);
    }
    public function countRollCoinBack($from_user_id, $begin_time = 0, $end_time = 0)
    {
        $where = [
            'from_user_id' => intval($from_user_id),
            'is_coin'      => 1,
            'check_type'   => 0,
        ];
        if ($begin_time) {
            $where[] = ['create_time' => ['>', intval($begin_time)]];
        }
        if ($begin_time) {
            $where[] = ['create_time' => ['<', intval($end_time)]];
        }
        $field = [
            'to_user_id',
            ['sum(`total_diamonds`) total_diamonds'],
            ['sum(`total_ticket`) total_ticket'],
        ];
        return $this->table('video_prop_' . date('Ym'))->field($field)->selectOne($where);
    }
    public function rollCoinBack($from_user_id, $begin_time = 0, $end_time = 0)
    {
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/BaseRedisService.php');
        $where = [
            'from_user_id' => intval($from_user_id),
            'is_coin'      => 1,
            'check_type'   => 0,
        ];
        if ($begin_time) {
            $where[] = ['create_time' => ['>', intval($begin_time)]];
        }
        if ($begin_time) {
            $where[] = ['create_time' => ['<', intval($end_time)]];
        }
        $field = [
            'to_user_id',
            ['sum(`total_diamonds`) total_diamonds'],
            ['sum(`total_ticket`) total_ticket'],
        ];
        $res = $this->table('video_prop_' . date('Ym'))->field($field)->group('to_user_id')->select($where);

        $user_model     = self::build('user');
        $coin_log_model = self::build('coin_log');
        $log_model      = self::build('user_log');

        $ids   = [$from_user_id];
        $coins = 0;
        foreach ($res as $value) {
            // 退还游戏币
            $money = $value['total_diamonds'];
            $user_model->coin($from_user_id, $money);
            $account_diamonds = $user_model->coin($from_user_id);
            $coin_log_model->addLog($from_user_id, -1, $money, $account_diamonds, '游戏币礼物退还');
            // 印票退还
            $total_ticket = intval($value['total_ticket']);
            $user_model->update(['ticket' => ['ticket - ' . $total_ticket]], ['id' => $value['to_user_id']]);
            $log_data = [
                'log_time'     => NOW_TIME,
                'log_admin_id' => 0,
                'money'        => 0,
                'type'         => 8,
                'prop_id'      => 0,
                'score'        => 0,
                'point'        => 0,
                'podcast_id'   => 0,
                'diamonds'     => 0,
                'video_id'     => 0,
                'log_info'     => '游戏币礼物退还',
                'user_id'      => $value['to_user_id'],
                'ticket'       => -$total_ticket,
            ];
            $log_model->insert($log_data);

            $ids[] = $value['to_user_id'];
            $coins += $money;
        }
        self::build('video_prop_' . date('Ym'))->update(['check_type' => 1], $where);
        user_deal_to_reids($ids);
        return $coins;
    }
}
